package zengjin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zengjin.common.PageResponse;
import zengjin.common.R;
import zengjin.model.dto.employer.PageEmployerRequest;
import zengjin.model.dto.employer.UpdateEmployerPersonalDto;
import zengjin.model.en.Employer;
import zengjin.model.en.Image;
import zengjin.model.vo.employer.EmployerPersonalVO;
import zengjin.model.vo.employer.EmployerVO;
import zengjin.service.EmployerService;
import zengjin.service.ImageService;

/**
 * @author 19324
 */
@Tag(name = "雇主相关接口")
@RestController
@RequestMapping("/employer")
@Slf4j
public class EmployerController {


    @Resource
    private EmployerService employerService;


    @Resource
    private ImageService imageService;

    //1.查询雇主个人信息
    @Operation(summary = "查询雇主个人信息")
    @PostMapping("/queryEmployerPersonal")
    public R<EmployerPersonalVO> queryEmployerPersonal(Long employerId) {
        //1.参数校验
        if (employerId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Employer employer = employerService.getById(employerId);
        EmployerPersonalVO employerPersonalVO = BeanUtil.copyProperties(employer, EmployerPersonalVO.class);
        //3.查询头像
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name", "employer_avatar");
        imageQueryWrapper.eq("image_name_id", employerId);
        Image image = imageService.getOne(imageQueryWrapper);
        if (image != null) {
            employerPersonalVO.setUserAvatarUrl(image.getImageUrl());
        }
        return R.success(employerPersonalVO, "查询成功");
    }

    //2.修改雇主个人信息
    @Operation(summary = "修改雇主个人信息")
    @PostMapping("/updateEmployerPersonal")
    public R<Boolean> updateEmployerPersonal(@RequestBody UpdateEmployerPersonalDto employerPersonalDto) {
        //1.参数校验
        if (employerPersonalDto == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean result = employerService.updateEmployerPersonal(employerPersonalDto);
        if (!result) {
            throw new RuntimeException("修改失败");
        }
        return R.success(true, "修改成功");
    }

    //3.查询雇主信息根据ID
    @Operation(summary = "查询雇主信息根据ID")
    @PostMapping("/getEmployerDetailByEmployerId")
    public R<EmployerVO> getEmployerDetailByEmployerId(Long employerId) {
        //1.参数校验
        if (employerId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Employer employer = employerService.getById(employerId);
        EmployerVO employerVO = BeanUtil.copyProperties(employer, EmployerVO.class);
        //查询出头像
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name", "employer_avatar");
        imageQueryWrapper.eq("image_name_id", employerId);
        Image image = imageService.getOne(imageQueryWrapper);
        if (image != null) {
            employerVO.setUserAvatarUrl(image.getImageUrl());
        }
        return R.success(employerVO, "查询成功");
    }

    //4.管理员-修改雇主状态
    @Operation(summary = "管理员-修改雇主状态")
    @PostMapping("/updateEmployerStatus")
    public R<Boolean> updateEmployerStatus(Long employerId, String employerStatus) {
        //1.参数校验
        if (employerId == null || employerStatus == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        UpdateWrapper<Employer> employerUpdateWrapper = new UpdateWrapper<>();
        employerUpdateWrapper.eq("employer_id", employerId)
                .set("employer_status", employerStatus);
        boolean result = employerService.update(null, employerUpdateWrapper);
        if (!result) {
            return R.error(false, "修改雇主状态失败");
        }
        return R.success(true, "修改雇主状态成功");
    }

    //5.管理员-分页获取雇主信息
    @Operation(summary = "管理员-分页获取雇主信息")
    @PostMapping("/getEmployerAdminPage")
    public R<PageResponse<EmployerVO>> getEmployerAdminPage(@RequestBody PageEmployerRequest pageEmployerRequest) {
        //1.参数校验
        if (pageEmployerRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<EmployerVO> employerAdminPage = employerService.getEmployerAdminPage(pageEmployerRequest);
        return R.success(employerAdminPage, "查询成功");

    }

    //6.管理员-修改雇主认证状态
    @Operation(summary = "管理员-修改雇主认证状态")
    @PostMapping("/updateEmployerVerified")
    public R<Boolean> updateEmployerVerified(Long employerId, String employerIsVerified) {
        //1.参数校验
        if (employerId == null || employerIsVerified == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean result = employerService.updateEmployerVerified(employerId, employerIsVerified);
        if (!result) {
            return R.error(false, "修改雇主认证状态失败");
        }
        return R.success(true, "修改雇主认证状态成功");
    }

}
